package com.zxc.csms.util;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJBFactory {
	
	private static Context context;
	//私有化构造
	protected EJBFactory(){}
	/**
	 * 获取远程Service Bean
	 * @param appName
	 * @param moduleName
	 * @param distinctName
	 * @param className
	 * @param completeInterfaceName
	 * @return
	 * @throws NamingException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getRemoteStatelessBean(final String appName, final String moduleName,
			final String distinctName, final String className,
			final String completeInterfaceName) throws NamingException {
		StringBuilder name = new StringBuilder();
		name.append("ejb:");
		name.append(appName);
		name.append("/");
		name.append(moduleName);
		name.append("/");
		name.append(distinctName);
		name.append("/");
		name.append(className);
		name.append("!");
		name.append(completeInterfaceName);
		if (context == null) {
			Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
			jndiProperties.put(Context.URL_PKG_PREFIXES,
					"org.jboss.ejb.client.naming");
			context = new InitialContext(jndiProperties);
		}
		T serviceBean = (T) context.lookup(name.toString());
		return serviceBean;
	}
}
